//    Copyright (C) Kherty.  All rights reserved.
using System;
using OpenLS.Core.Serialization;
using OpenLS.Drawing;

namespace OpenLS.Spreadsheet
{
    public class GradientStop: IOfficeXmlSerializable
    {
        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>. </param><filterpriority>2</filterpriority>
        public override bool Equals(object obj)
        {
            if (obj is GradientStop)
                return Equals(((GradientStop) obj));
            return false;
        }
        bool Equals (GradientStop gradientStop)
        {
            if (gradientStop._position != _position)
                return false;
            if (gradientStop.Color == null && Color == null)
                return true;
            if (gradientStop.Color == null)
                return false;
            if (Color == null)
                return false;
            return (gradientStop._color.Equals(this._color));

        }

        /// <summary>
        /// Serves as a hash function for a particular type. 
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        /// <filterpriority>2</filterpriority>
        public override int GetHashCode()
        {
            return this._position.GetHashCode() + _color.GetHashCode();
        }
        #region IOfficeXmlSerializable Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (ReadContext c = context.Read("stop"))
            {
                Position = c.GetDouble("position");
                Color = SpreadsheetColor.ReadColor("color", c);

            }
        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write("stop"))
            {
                c.SetDouble("position", Position);
                Color.WriteXmlAs("color", c);
            }
        }

        #endregion
        private SpreadsheetColor _color;

        public SpreadsheetColor Color
        {
            get { return _color; }
            set { _color = value; }
        }
        private double _position;

        public double Position
        {
            get { return _position; }
            set { _position = value; }
        }



        internal System.Windows.Media.GradientStop ToWPFGradientStop(ColorScheme colorScheme)
        {
            return new System.Windows.Media.GradientStop(Color.GetColor(colorScheme), Position);
        }

        public GradientStop Clone()
        {
            return new GradientStop() {_position = _position, _color = _color}; //\\ clone color?
        }
    }
}
